package view;

import java.io.InputStream;
import java.util.Locale;
import java.util.ResourceBundle;

import model.ImageSelector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import view.listener.CloseButtonListener;
import view.listener.InfoBoxListener;
import view.listener.NewGameButtonListener;
import control.Storage;
import control.Util;

public class PairFrame {

	private Display display;
	private Shell shell;
	private int x = Storage.getInstance().getSize();
	private ResourceBundle messages;
	private Label currentPlayer;
	private Label turnCount;
	private Menu menu;
	private MenuItem fileTitle;
	private Menu fileMenu;
	private MenuItem game;
	private MenuItem exit;
	private MenuItem infoTitle;
	private Menu infoMenu;
	private MenuItem info;
	private MenuItem languageTitle;

	public PairFrame() {

		display = Display.getCurrent();
		shell = new Shell(display);
		this.messages = Storage.getInstance().getMessages();
		Storage.getInstance().setNewGame(false);
		configureShell();
		keepAlive();
	}

	private void buildContent() {

		ImageSelector.getInstance().buildImages();
		setIcons();
		createMenuBar();

		currentPlayer = new Label(shell, SWT.NONE);
		GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false, x, 1);
		currentPlayer.setLayoutData(data);

		currentPlayer.setText(messages.getString("CURRENTPLAYER")
				+ Storage.getInstance().getPlayerName());

		Storage.getInstance().setCurrentPlayer(currentPlayer);

		for (int i = 0; i < (x * x); i++) {

			new Card(shell, i);

		}

		turnCount = new Label(shell, SWT.NONE);

		turnCount.setText(messages.getString("CURRENTTURNSCOUNT")
				+ Storage.getInstance().getTurnsCount());

		Storage.getInstance().setTurnCount(turnCount);

	}

	private void setIcons(){
		
		InputStream in3 = this.getClass().getResourceAsStream("/res/card_16.png");
		final Image small = new Image(display, in3);
		InputStream in4 = this.getClass().getResourceAsStream("/res/card_32.png");
		final Image large = new Image(display, in4);
		final Image[] images = new Image[] { small, large };
		shell.setImages(images);
	}
	
	private void configureShell() {
		shell.setLayout(getShellLayout());
		shell.setText(messages.getString("MEMORYTITLE"));
		buildContent();
		shell.pack();
		Util.centerFrame(shell);
		shell.setVisible(true);
		shell.forceFocus();
	}

	private void createMenuBar() {

		 menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		fileTitle = new MenuItem(menu, SWT.CASCADE);
		fileTitle.setText(messages.getString("FILE"));
		
		fileMenu = new Menu(shell, SWT.DROP_DOWN);
		fileTitle.setMenu(fileMenu);

		game = new MenuItem(fileMenu, SWT.NULL);
		game.setText("&" + messages.getString("NEWGAME") + "\tF2");
		game.setAccelerator(SWT.F2);
		game.addSelectionListener(new NewGameButtonListener(shell));

		exit = new MenuItem(fileMenu, SWT.NULL);
		exit.setAccelerator(SWT.ALT + SWT.F4);
		exit.setText("&" + messages.getString("CLOSEBUTTONLABEL") + "\tAlt+F4");
		
		exit.addSelectionListener(new CloseButtonListener(shell));

		infoTitle = new MenuItem(menu, SWT.CASCADE);
		infoTitle.setText(messages.getString("INFO"));
		

		infoMenu = new Menu(shell, SWT.DROP_DOWN);
		infoTitle.setMenu(infoMenu);

		info = new MenuItem(infoMenu, SWT.NULL);
		info.setAccelerator(SWT.F1);
		info.setText("&" + messages.getString("INFORMATION") + "\tF1");
		info.addSelectionListener(new InfoBoxListener());
		
		languageTitle = new MenuItem(menu, SWT.CASCADE);
		languageTitle.setText(messages.getString("LANGUAGE"));
		languageTitle.addSelectionListener(new SelectionListener() {

	        @Override
	        public void widgetSelected(SelectionEvent e) {
	        	changeLanguage(messages.getString("LANGUAGE"));
	        }

	        @Override
	        public void widgetDefaultSelected(SelectionEvent e) {
	            // TODO Auto-generated method stub
	        		
	        }
	    });

	}

	private void changeLanguage(String language){
		Locale currentLocale;
		if(language.equals("Magyar"))
		{
			currentLocale = new Locale("hu","HU" );
			Storage.getInstance().setMessages(ResourceBundle.getBundle("res/MessagesBundle", currentLocale));
			messages = Storage.getInstance().getMessages();
			setAll();
		}
		if(language.equals("English"))
		{
			currentLocale = new Locale("en","EN" );
			Storage.getInstance().setMessages(ResourceBundle.getBundle("res/MessagesBundle", currentLocale));
			messages = Storage.getInstance().getMessages();
			setAll();
		}
		
	}
	private void setAll(){
		currentPlayer.setText(messages.getString("CURRENTPLAYER")
				+ Storage.getInstance().getPlayerName());
		
		turnCount.setText(messages.getString("CURRENTTURNSCOUNT")
				+ Storage.getInstance().getTurnsCount());
		
		shell.setText(messages.getString("MEMORYTITLE"));
		
		fileTitle.setText(messages.getString("FILE"));
		fileTitle.setText(messages.getString("FILE"));

		game.setText(messages.getString("NEWGAME"));
		
		exit.setText(messages.getString("CLOSEBUTTONLABEL"));
		
		infoTitle.setText(messages.getString("INFO"));
		
		info.setText(messages.getString("INFORMATION"));
		
		languageTitle.setText(messages.getString("LANGUAGE"));
		shell.pack();

	}
	private Layout getShellLayout() {
		GridLayout layout = new GridLayout(x, true);
		layout.marginLeft = 10;
		layout.marginRight = 10;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		return layout;
	}

	private void keepAlive() {

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}

}
